<template>
  <el-drawer v-model="isDrawer" direction="rtl" :title="title" :footer="false" class="w-1/4">
    <el-form :model="formParams" ref="formRef" label-placement="left" :label-width="80">
      <el-form-item label="头像" prop="face">
        <TableImg :imgList="[formParams.face]" v-if="formParams.face" class="w-20" />
        <el-avatar v-else :size="48">
          <el-icon>
            <UserOutlined />
          </el-icon>
        </el-avatar>
      </el-form-item>
      <el-form-item label="登录账号" prop="uid">
        {{ formParams.uid }}
      </el-form-item>

      <el-form-item label="角色">
        <el-space wrap>
          <el-tag v-for="item in formParams.roles" :key="item.id">
            {{ item.name }}
          </el-tag>
        </el-space>
      </el-form-item>

      <el-form-item label="所在岗位">
        {{ formParams.post?.name || '无' }}
      </el-form-item>

      <el-form-item label="所在部门">
        {{ formParams.dept?.name || '无' }}
      </el-form-item>

      <el-form-item label="姓名" prop="realName">
        {{ formParams.realName }}
      </el-form-item>

      <el-form-item label="手机" prop="mobile">
        {{ formParams.mobile }}
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        {{ formParams.email }}
      </el-form-item>

      <el-form-item label="性别" prop="gender">
        <el-tag :type="formParams.gender === 'MALE' ? '' : 'danger'">
          {{ formParams.gender === 'MALE' ? '男' : '女' }}
        </el-tag>
      </el-form-item>

      <el-form-item label="状态" prop="disabled">
        <el-Tag :type="formParams.disabled ? 'danger' : 'success'">{{
          formParams.disabled ? '禁用' : '启用'
        }}</el-Tag>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>

<script lang="ts" setup>
  import { ref, onMounted } from 'vue';
  import { userInfo } from '@/api/system/user';
  import { postList } from '@/api/auth/post';
  import { deptList } from '@/api/auth/dept';
  import { roleList } from '@/api/system/role';
  import { TableImg } from '@/components/TableImg';
  import { UserOutlined } from '@vicons/antd';

  defineProps({
    title: {
      type: String,
      default: '详情',
    },
  });

  const roleLoading = ref(false);
  const roleOption = ref();
  const postOption = ref();
  const deptOption = ref();

  const formRef: any = ref(null);
  const isDrawer = ref(false);
  const postLoading = ref(false);
  const deptLoading = ref(false);

  const formParams = ref<any>({});

  async function openDrawer(id?) {
    if (id) {
      formParams.value.id = id;
      getInfo();
      return;
    }
    isDrawer.value = true;
  }

  function closeDrawer() {
    isDrawer.value = false;
  }

  function getInfo() {
    userInfo(formParams.value.id).then(async (res) => {
      formParams.value = res.user;
      isDrawer.value = true;
    });
  }

  async function getRoleData(params?) {
    const res = await roleList(params);
    roleOption.value = res.list;
    roleLoading.value = false;
  }

  async function getPostData(params?) {
    const res = await postList(params);
    postOption.value = res.list;
    postLoading.value = false;
  }

  async function getDeptData(params?) {
    const res = await deptList(params);
    deptOption.value = res.list;
    deptLoading.value = false;
  }

  onMounted(async () => {
    getRoleData();
    getPostData();
    getDeptData();
  });

  defineExpose({
    openDrawer,
    closeDrawer,
  });
</script>
